package ro.fwd.transitions{
	import flash.display.Sprite;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.events.EventDispatcher
	
	
	public class Dissolve extends Sprite{
		public static const TRANSITION_DONE:String = "transition_done";
		private var _b1:BitmapData;
		private var _b2:BitmapData;
		private var _seed:Number;
		private var _pixelCount:Number=0;
		private var _numPixel:Number=0;
		private var _holder:Sprite;
		private var _transitionBitmap:Bitmap;
		
		public function Dissolve(b1:BitmapData,b2:BitmapData){
			_b1 =  b1;
			_b2 =  b2;
			trace(_b1);
			trace(_b2);
			_transitionBitmap = new Bitmap(_b1);
			_holder =  new Sprite();
			_holder.addChild(_transitionBitmap);
			addChild(_holder);
			_seed = Math.random() * 100000;
			_numPixel = (b1.width * b1.height)/70;
			addEventListener(Event.ENTER_FRAME,onEF);
		}
		
		private function onEF(e:Event):void{
			_seed = _b1.pixelDissolve(_b2,_b1.rect,new Point(),_seed,_numPixel,0x00000000);
		    _pixelCount += _numPixel;
		    if(_pixelCount > _b1.width * _b1.height){
		    	removeEventListener(Event.ENTER_FRAME,onEF);
				_b1.dispose();
				_b2.dispose();
				_holder.removeChild(_transitionBitmap);
				removeChild(_holder);
				dispatchEvent(new Event(Dissolve.TRANSITION_DONE));
				//trace("DISOLVE DONE AND DESTROYED");
		    }	
		}
	}
}